package com.aruba.xfermodeapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圆角图片
 */
public class RoundImageview extends View {
    private Bitmap bitmapSrc;
    private Bitmap bitmapDst;
    private Paint mPaint = new Paint();

    public RoundImageview(Context context) {
        this(context, null);
    }

    public RoundImageview(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //使用Xfermode时，关闭硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        bitmapSrc = BitmapFactory.decodeResource(getResources(), R.mipmap.xyjy6);
        bitmapDst = BitmapFactory.decodeResource(getResources(), R.mipmap.shade);

        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawBitmap(bitmapDst,0,0,mPaint);
        //后画SRC的图片
        Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaint.setXfermode(xfermode);
        canvas.drawBitmap(bitmapSrc,0,0,mPaint);

        mPaint.setXfermode(null);
    }
}
